package com.love.cloud.gateway.controller;

import cn.hutool.core.io.resource.ResourceUtil;
import com.love.cloud.common.captcha.callback.CacheCallback;
import com.love.cloud.common.captcha.core.CaptchaResult;
import com.love.cloud.common.captcha.core.CaptchaVo;
import com.love.cloud.common.captcha.core.PuzzleCaptcha;
import com.love.cloud.common.captcha.util.CaptchaUtil;
import com.love.cloud.common.core.constant.CacheConstants;
import com.love.cloud.common.core.constant.SecurityConstants;
import com.love.cloud.common.core.exception.ValidateCodeException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.awt.*;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @author hongdongqu
 */
@RestController
@RequestMapping("/drag/code")
@CrossOrigin
public class DragCodeController {
    @Resource
    private RedisTemplate redisTemplate;

    @GetMapping("/getSliderCode")
    public Object getSliderCode(String randomStr){
        //设置源图片
        PuzzleCaptcha puzzleCaptcha=new PuzzleCaptcha(ResourceUtil.getStream("images/captcha/default.jpg"));
        //设置画质
        puzzleCaptcha.setImageQuality(Image.SCALE_AREA_AVERAGING);
        //执行分割图片
        puzzleCaptcha.run();
        //获取到验证码图片
        CaptchaVo captcha = CaptchaUtil.captcha(puzzleCaptcha, randomStr, this.getCacheCallback());
        return captcha;
    }

    @PostMapping("/verifyCode")
    public Object verifyCode(@RequestBody Map<String, Object> map){
        String randomStr=map.get("randomStr")!=null?map.get("randomStr").toString():"";
        Map<String,Object> resultMap=new HashMap<>();
        if(StringUtils.isEmpty(randomStr)){
            throw new ValidateCodeException("请传入正确的校验码id");
        }
        CaptchaResult verify = CaptchaUtil.verify(map, randomStr,this.getCacheCallback());
        if(!verify.isSuccess()){
            resultMap.put("success",false);
            return resultMap;
        }
        //产生一个随机码然后保存到缓存中
        long date = System.currentTimeMillis();//加入毫秒级的后缀
        String uuid = UUID.randomUUID().toString().replace("-", "")+date;
        //保存到redis中
        if(com.alibaba.cloud.commons.lang.StringUtils.isNotBlank(uuid)){
            //设置1分钟过期
            redisTemplate.opsForValue().set(CacheConstants.DEFAULT_CODE_KEY+randomStr,uuid,5, TimeUnit.MINUTES);
        }
        resultMap.put("validateCode",uuid);
        resultMap.put("success",true);
        return resultMap;
    }

    private CacheCallback getCacheCallback(){
        return new CacheCallback() {
            @Override
            public void removeCache(String cacheKey) {
                redisTemplate.delete(cacheKey);
            }

            @Override
            public void putCache(String cacheKey, Object value, long exp, TimeUnit timeUnit) {
                redisTemplate.opsForValue().set(cacheKey, value,
                        SecurityConstants.CODE_TIME, TimeUnit.SECONDS);
            }

            @Override
            public Map<String, Object> getCache(String cacheKey) {
                Object o = redisTemplate.opsForValue().get(cacheKey);
                if (o instanceof Map) {
                    return (Map) o;
                }
                return null;
            }
        };
    }
}
